<template>
  <div class="la-panel" :class="panelClass">
    <div class="la-panel__header" v-if="!hideTitle" @dblclick="handleDblClick">
      <i class="la-panel__icon" :class="panelIcon"></i>

      <div v-if="!collapse" style="display: inline-block;">
        <slot v-if="$slots.title" name="title"/>
        <span v-else>{{ title }}</span>
      </div>
      <span v-if="collapse" class="la-panel__colbtn" @click="handleCollapse">
        <i class="la-panel__colbtn__icon el-icon-d-arrow-right"/>
        <span>{{ title }}</span>
      </span>
      <el-popover v-if="$slots.help" placement="top" v-model="showHelp" popper-class="la-panel-help-popper">
        <div>
          <slot name="help"/>
        </div>
        <template slot="reference">
          <i style="color:#2d8cf0;cursor:pointer;margin-left:4px" class="el-icon-question"/>
        </template>
      </el-popover>
      <div class="la-panel__buttons">
        <slot name="buttons"/>
        <el-button v-if="maxable"
                   :icon="state!=='maximize'?'el-icon-fa-window-maximize':'el-icon-fa-window-minimize'"
                   type="text"
                   title="点击最大化" @click="handleMaximize"/>
      </div>
      <div style="clear:both"></div>
    </div>
    <div class="la-panel__body ">
      <slot/>
      <div style="clear:both"></div>
    </div>
    <div style="clear:both"></div>

  </div>
</template>

<script lang="ts">
import {defineComponent} from 'vue'

const panelManager = {
  idx: 0,
  next(): number {
    ++this.idx;
  }
};

export default defineComponent({
  name: "Panel",
  props: {
    maxable: {type: Boolean, default: false},
    collapse: {type: Boolean, default: false},
    light: {type: Boolean, default: false},
    icon: {type: String},
    full: {type: Boolean, default: false},
    title: {type: String},
    hideTitle: {type: Boolean, default: false},
  },
  computed: {
    panelIcon() {
      return this.light && !this.collapse ? "el-icon-d-arrow-right" : this.icon;
    },
    panelClass() {
      return [
        {
          "la-panel--full": this.full,
          "la-panel--collapse": this.collapse,
          "la-panel--light": this.light,
          "la-panel--icon": !!this.icon
        },
        "la-panel--" + this.state
      ];
    }
  },
  data() {
    return {
      showHelp: false,
      state: "opened"
    };
  },
  beforeMount() {
    if (this.collapse) {
      this.state = "closed";
    }
  },
  mounted() {
    this.$el.tabindex = panelManager.next();
    this.$el.addEventListener("keydown", e => {
      if (e.keyCode == 112) {
        if (this.$slots.help) {
          this.showHelp = !this.showHelp;
        }
        e.preventDefault();
      }
    });


  },
  methods: {
    handleCollapse() {
      if (this.state === "closed") {
        this.state = "opened";
        this.$emit('opened');
      } else {
        this.state = "closed";
        this.$emit('closed');
      }
    },
    handleMaximize() {
      // 允许外部手动触发放大
      this.state = this.state === "maximize" ? "" : "maximize";
    },

    handleDblClick() {
      // 限制双击放大的触发条件
      if (!this.maxable) {
        return;
      }
      this.handleMaximize();
    }
  }
});
</script>

<style lang="scss">

@import "../theme/variable.scss";

.la-panel {
  background: #fff;
  box-sizing: border-box;
  border: 1px solid #bfcbd9;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  outline: none;
  border-radius: 4px;

  &__icon {
    color: #20a0ff;
  }

  &--full {
    min-height: 100%;
  }

  &:focus,
  &--focus {
    border-color: #6eabf0;
  }

  &--maximize {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    z-index: 99999999999;
  }

  &__buttons {
    float: right;
    margin-top: 0;
    margin-right: 6px;

    .el-button {
      padding: 3px 6px;
      font-size: 12px;
      min-height: 24px;
    }
  }

  &__header {
    color: #000;
    border-bottom: 1px solid #bfcbd9;
    cursor: default;
    background-color: #d6dbe2;
    font-size: 14px;
    font-weight: bold;
    line-height: 32px;
    height: 32px;
    padding-left: 6px;
  }

  &__body {
    flex: 1;
    padding: 3px;
    transition: height 0.5s;
    height: 100%;
    box-sizing: border-box;
  }

  //变种区-----------------
  &--collapse {
    .la-panel__header {
      color: #20a0ff;

      &:before {
        content: unset;
      }
    }
  }

  &__colbtn {
    cursor: pointer;

    &__icon {
      margin-right: 4px;
      transform: rotate(90deg);
      transition: transform 0.5s;
    }
  }

  &--light {
    border: none;

    .la-panel__header {
      background-color: #ebf1f3d7;
      border-bottom: none;

      &:before {
        content: unset;
      }
    }
  }

  &--icon {
    .la-panel__header {
      &:before {
        content: unset;
      }
    }
  }

  &--tableFooter {
    border: 1px solid #EBEEF5;
    padding: 3px;
    background-color: #f8fdff;
    border-top-width: 0;

    .el-pagination {
      float: right;
      font-weight: 500;
      padding: 0 0 0 0;

      button,
      span:not([class*="suffix"]) {
        height: $--line-height;
        line-height: $--line-height;
        min-width: 20px;
        margin: 0;
        padding: 0;
      }

      .el-input__inner {
        height: $--line-height;
        line-height: $--line-height;
        padding: 0;
        background-color: #fff !important;
      }

      .el-select .el-input {
        margin: 1px 0;
      }

      .btn-prev,
      .btn-next {
        min-width: 20px;
        padding: 0 1px;
        margin: 1px;
      }

      .el-pager {
        height: $--line-height;
        line-height: $--line-height;

        li {
          min-width: 20px;
          margin: 1px;
          height: $--line-height;
          line-height: $--line-height;
        }

        .more::before {
          height: $--line-height;
          line-height: $--line-height;
        }
      }
    }
  }

  &--closed {
    .la-panel__header {
      border-bottom: none;

      &:before {
        content: unset;
      }

      .la-panel__colbtn__icon {
        transform: rotate(0);
      }
    }

    .la-panel__body {
      height: 0;
      padding: 0;

      * {
        display: none;
      }
    }
  }
}


.el-popper.la-panel-help-popper {
  background: #000;
  color: #fff;
  font-size: 12px;
  padding: 6px;

  &[x-placement^="top"] .popper__arrow::after {
    border-top-color: #000;
  }

  &[x-placement^="bottom"] .popper__arrow::after {
    border-bottom-color: #000;
  }

  &[x-placement^="left"] .popper__arrow::after {
    border-left-color: #000;
  }

  &[x-placement^="right"] .popper__arrow::after {
    border-right-color: #000;
  }
}
</style>